package gl.android.xmppandroidclient.db;

import gl.android.util.Lg;
import gl.android.xmppandroidclient.bean.BeanMessage;
import gl.android.xmppandroidclient.common.Constant;

import java.util.List;

import org.jivesoftware.smack.packet.Message;

import net.tsz.afinal.FinalDb;
import android.content.Context;

/**
 * 持久化存储通过XMPP服务发送和接收的消息
 * 
 * @author Administrator
 * 
 */
public class MessagePersistencer {
	FinalDb sqlhelper;

	public MessagePersistencer(Context context) {
		sqlhelper = FinalDb.create(context, Constant.DB_NAME);
	}

	public void save(Message message) {
		BeanMessage bm = new BeanMessage();
		bm.setM_messagebody(message.getBody());
		bm.setM_to(message.getTo());
		bm.setM_from(message.getFrom());
		bm.setM_time(System.currentTimeMillis());
		bm.setM_UUID(message.getPacketID());
		sqlhelper.save(bm);
	}

	public void update(Message message) {
	}

	public void select(String message) {
		List<BeanMessage> list = sqlhelper.findAll(BeanMessage.class);
		for (BeanMessage beanMessage : list) {
			Lg.i(this, beanMessage.getM_messagebody());
		}
	}

	public void destory() {
	}
}
